var express = require('express');
var router = express.Router();
var db = require('../model/index')

/* 查询管理员信息 */
router.get('/api/login', function (req, res, next) {
  console.log(req.query)
  let mysql = `SELECT * FROM admin AS t1 LEFT JOIN (SELECT id AS roleid,ENABLE as roleEnable,jurisdiction FROM role) AS t2 ON t1.roleid=t2.roleid WHERE username='${req.query.username}' AND passwords='${req.query.password}'`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询管理员失败，请稍后重试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '查询管理员成功',
      data: data
    })
  });
});
router.get('/login', function (req, res, next) {
  console.log(req.query)
  let mysql = `SELECT * FROM admin AS t1 LEFT JOIN (SELECT id AS roleid,ENABLE as roleEnable,jurisdiction FROM role) AS t2 ON t1.roleid=t2.roleid WHERE username='${req.query.username}' AND passwords='${req.query.password}'`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询管理员失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '查询管理员成功',
      data: data
    })
  });
});

/* 更改登陆时间 */
router.post('/logintime', function (req, res, next) {
  console.log(req.body)
  let mysql = `UPDATE admin SET logintime='${req.body.logintime}' WHERE id=${req.body.id};`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '更新登陆时间失败'
      })
    }
    res.json({
      code: 200,
      msg: '更新登陆时间成功',
      data: data
    })
  });
});
module.exports = router;